<?php

class rvg extends module {

	public $title = "Radiovazogasy Module";
	public $author = "Hery";
	public $version = "0.9";
	public $endpoint = "http://radiovazogasy.com/wsdl/?wsdl";
	private $wsid = "karajia";
	public $client;

	public function init()
	{
		$this->vonjy_ini = new ini("data/vonjy.ini");
		$this->vonjy_ini->setIniVal("radiovazogasy", "!rvg", "Mampiseho ny baiko ilaina amin'ny Radiovazogasy");
		$this->vonjy_ini->writeIni();
	}
	public function priv($line, $args)
	{

		if ($args['nargs'] == 0)
		{
			$arg = "help";
		}
		else
		{
			$arg = $args['arg1'];
		}
		switch ($arg)
		{
			case "help":
				$this->ircClass->privMsg($line['fromNick'], "Tonga soa eto amin'ny " . GREEN . "Radio" . BLUE . "Vazo" . RED . "Gasy" . NOFORMAT . ". ireto avy ny baiko azo ampiasaina");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg mandeha : " . NOFORMAT . "Mampiseho ny hira mandeha amin'izao fotoana izao");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg miandry : " . NOFORMAT . "Mampiseho ny hira handeha efa milahatra ao anaty lisitra");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg tadiavo " . BLUE . "<lohateny na mpihira> : " . NOFORMAT . "Mitady ny hira na mpihira". GREEN . " ohatra: " . NOFORMAT . "!rvg tadiavo Rija oelijaona");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg alaharo " . BLUE . "<laharana> : " . NOFORMAT . "Mandahatra ny hira voalazan'ny <laharana>". GREEN . " ohatra: " . NOFORMAT . "!rvg alaharo 654");
				$this->ircClass->privMsg($line['fromNick'], RED ."!rvg hafaro " . BLUE . "<laharana> <hafatra>" . NOFORMAT . "Mametraka ny hafatra <hafatra> amin'ny hira voalazan'ny <laharana>" . GREEN . " ohatra: " . NOFORMAT . "!rvg hafaro 65224 Misaotra anao");

			
			break;
			case "search":
			case "rechercher":
			case "tadiavo":

				if(!isset($args['arg2'])) {
					$this->ircClass->privMsg($line['fromNick'], "Tsy nametraka lohateny na mpihira hotadiavina ianao. Soraty ny baiko !rvg tadiavo <teny tadiavina>");
					Return ;
				} else {
					$tadiavo = trim(substr($args['query'], (strpos($args['query'], $args['arg1']) + (strlen($args['arg1'])))));
					
					
					
			       $service_url = 'http://radiovazogasy.com/radio/rest/';
			       $curl = curl_init($service_url);
			       $curl_post_data = array(
			            "tadiavo" => $tadiavo
			            );
			       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			       curl_setopt($curl, CURLOPT_POST, true);
			       curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
			       $curl_response = curl_exec($curl);
			       curl_close($curl);

			       $xml = new SimpleXMLElement($curl_response);
					
					print_r($xml);
	   
					if ($this->client->fault) {
						$this->ircClass->privMsg($line['fromNick'], "Tsy hita ny hira notadiavinao.");
						return;
					}
					if(is_array($results)) {
						$this->ircClass->privMsg($line['fromNick'], "Ireto ny hira hita. Izay misy isa dia azonao asiana alahatra amin'ny baiko !rvg alaharo");
						foreach($results as $result) {
							$msg = $result['artist'] . ", " . $result['title'] . "(" . $result['length'] . ") , " . RED . "!rvg alaharo " . $result['id'] ; 
							$this->ircClass->privMsg($line['fromNick'], $msg);
						}
						return;
					} else {
						$this->ircClass->privMsg($line['fromNick'], "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
						return;
					}
				}

			
			break;
			case "alaharo":
				if(!$user = $this->getModule("user")->loggedin($line))
				{
					$this->getModule("user")->printError($line);
					return;
				}
				
				if(!isset($args['arg2'])) {
					$this->ircClass->privMsg($line['fromNick'], "Tsy nametraka ny laharan'ny hira halahatra ianao. Soraty " . RED . "!rvg tadiavo <hira>" . NOFORMAT . " raha hitady hira");
					Return ;
				} elseif(intval($args['arg2']) == 0) {
					$this->ircClass->privMsg($line['fromNick'], "Tsy laharana ara-dalana io napetrakao io.");
					Return ;
				} else {
					$param = array('wsid' => $this->wsid, 'id' => intval($args['arg2']), 'username' => $user['username'] . " [karajia]");
					$result = $this->client->call('rvgAlaharo', $param);
					if ($this->client->fault) {
						$this->ircClass->privMsg($line['fromNick'], "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy. ");
						return;
					}
					if($result) {
						$this->ircClass->privMsg($line['fromNick'], BOLD . "Valiny: " . BOLD .$result );
						return;
					} else {
						$this->ircClass->privMsg($line['fromNick'], "Tsy tafiditra ny hira... Nisy olana");
						return;
					}
				}
			
			break;
			case "hafaro":

				if(!$user = $this->getModule("user")->loggedin($line))
				{
					$this->getModule("user")->printError($line);
					return;
				}

				if(!isset($args['arg3'])) {
					$this->ircClass->privMsg($line['fromNick'], "Diso ny baiko napetrakao. Ny baiko marina dia " . BOLD . "!rvg hafaro <laharana> <hafatra>" . BOLD . " (ka ny laharana dia ilay laharan'ny hira ao amin'ny !rvg miandry");
					Return ;
				} elseif(intval($args['arg2']) == 0) {
					$this->ircClass->privMsg($line['fromNick'], "Diso ny baiko napetrakao. Ny baiko marina dia " . BOLD . "!rvg hafaro <laharana> <hafatra>" . BOLD . " (ka ny laharana dia ilay laharan'ny hira ao amin'ny !rvg miandry");
					Return ;
				} else {
					$msg = trim(substr($args['query'], (strpos($args['query'], $args['arg2']) + (strlen($args['arg2'])))));
					$param = array('wsid' => $this->wsid, 'id' => intval($args['arg2']), 'username' => $user['username'], 'msg' => $msg);
					$result = $this->client->call('rvgHafaro', $param);
					if ($this->client->fault) {
						$this->ircClass->privMsg($line['fromNick'], "Tsy maharay na inona na inona aho... angamba tsy mandeha ny RadioVazoGasy. ");
						return;
					}
					if($result != "") {
						$this->ircClass->privMsg($line['fromNick'], $result);
						return;
					} else {
						$this->ircClass->privMsg($line['fromNick'], "Nisy hadisoana, tsy lasa ny hafatra");
						return;
					}
					Return ;
				}


			
			break;
			case "playing":
			case "mandeha":
				$param = array('wsid' => $this->wsid);
				$result = $this->client->call('rvgHira', $param);
				

				if ($this->client->fault) {

					$this->ircClass->privMsg($line['fromNick'], "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
					return;
				} else {
				
					if (is_array($result))
					{
						$this->ircClass->privMsg($line['fromNick'], BOLD . "Hira mandeha : " . BOLD . $result['title'] . ($result['artist'] ? " - " . $result['artist'] : ""));
						if(trim($result['album']) != "") {
							$this->ircClass->privMsg($line['fromNick'], BOLD . "Rakipeo : " . BOLD . $result['album']);						
						}
						if(trim($result['requester']) != "") {
							$this->ircClass->privMsg($line['fromNick'], BOLD . "Nangataka : " . BOLD . $result['requester']);						
						}
						if(trim($result['username']) != "") {
							$this->ircClass->privMsg($line['fromNick'], BOLD . "Nametraka hafatra : " . BOLD . $result['username']);						
						}
						if(trim($result['msg']) != "") {
							$this->ircClass->privMsg($line['fromNick'], BOLD . "Hafatra : " . BOLD .  $result['msg']);						
						}
						return;
					}
				}

			
			break;
			case "miandry":
			case "queue":

				$param = array('wsid' => $this->wsid);
				$results = $this->client->call('rvgHandeha', $param);
				if ($this->client->fault) {
					$this->ircClass->privMsg($line['fromNick'], "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
					return;
				}

				if(is_array($results)) {
					$this->ircClass->privMsg($line['fromNick'], "Ireto ny hira handeha rehefa avy eo. Izay misy isa dia azonao asiana hafatra amin'ny baiko " . RED . "!rvg hafaro <isa>");
					foreach($results as $result) {
						$msg = $result['artist'] . ", " . $result['title'] . "(" . $result['length'] . ") ";
						if($result['requester']) {
							$msg .= "- " . GREEN . "Nalahatr'i : " . $result['requester'];
						}
						if($result['id'] > 0 && $result['msg'] == "") {
							$msg .= ", " . RED . "!rvg hafaro " . $result['id'] ;
						} 
						$this->ircClass->privMsg($line['fromNick'], $msg);
					}
					return;
				} else {
					$this->ircClass->privMsg($line['fromNick'], "Tsy misy hira voaray.");
					return;
				}

			
			break;
		}
	}

}

?>
